package com.qc31.monitorlib.clusters

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.qc31.comlib.databinding.MarkerViewBinding
import com.qc31.comlib.map.MapMarker

/** @author joker @date 2022/7/22 11:29$ */
class MarkerView(
    context: Context,
    markerSet: MapMarker
) : LinearLayout(context) {
    val binding: MarkerViewBinding =
        MarkerViewBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        initView(markerSet)
    }

    private fun initView(markerSet: MapMarker) {
        if (markerSet.showSpeed) {
            binding.tvMarkerSpeed.visibility = View.VISIBLE
            binding.tvMarkerSpeed.textSize = markerSet.textSize.toFloat()

        } else {
            binding.tvMarkerSpeed.visibility = View.GONE
        }

        val layoutParams = binding.ivMarkerLogo.layoutParams
        layoutParams.width = markerSet.iconSize
        layoutParams.height = markerSet.iconSize
        binding.ivMarkerLogo.layoutParams = layoutParams

        binding.tvMarkerTitle.setTextColor(Color.parseColor(markerSet.textColor))
        binding.tvMarkerSpeed.setTextColor(Color.parseColor(markerSet.textColor))
        binding.tvMarkerTitle.setBackgroundColor(Color.parseColor(markerSet.backCarColor))
        binding.tvMarkerSpeed.setBackgroundColor(Color.parseColor(markerSet.backSpeedColor))
    }

    /**
     * 设置图标参数
     *
     * @param plate
     * @param iconLink
     * @param speed
     * @param state
     * @param drct
     */
    fun setMarkerInfo(
        plate: String, iconLink: String, speed: String, state: String, drct: String
    ) {
        binding.tvMarkerTitle.text =plate
        binding.ivMarkerLogo.setImageBitmap(BitmapUtil.getBitmap(context, iconLink))
        binding.tvMarkerSpeed.text = speed + "km/h"
        if (state.toInt() < 9) {
            binding.ivMarkerLogo.rotation = drct.toFloat()
        } else {
            binding.ivMarkerLogo.rotation = 0f
        }
    }

    /**
     * 设置图标参数
     *
     * @param clusterItem
     * @param isName
     */
    fun setMarkerInfo(clusterItem: ClusterItem, isName: Boolean){
        binding.tvMarkerTitle.text = if (isName) clusterItem.name else clusterItem.plate
        binding.ivMarkerLogo.setImageBitmap(BitmapUtil.getBitmap(context, clusterItem.iconLink))
        binding.tvMarkerSpeed.text = clusterItem.speed + "km/h"
        if (clusterItem.state.toInt() < 9) {
            binding.ivMarkerLogo.rotation = clusterItem.drct.toFloat()
        } else {
            binding.ivMarkerLogo.rotation = 0f
        }
    }

}